<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年级" prop="grade">
        <el-select
          v-model="queryParams.grade"
          placeholder="年级"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.grade"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="学科" prop="subject">
        <el-select
          v-model="queryParams.subject"
          placeholder="学科"
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in dict.type.subject"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:paper:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:paper:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:paper:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:paper:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="paperList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="名称" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <router-link :to="'/base/topic/index/' + scope.row.paperId" class="link-type">
            <span>{{ scope.row.name }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="年级" align="center" prop="grade" width="200">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.grade" :value="scope.row.grade"/>
        </template>
      </el-table-column>
      <el-table-column label="学科" align="center" prop="subject" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.subject" :value="scope.row.subject"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:paper:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:paper:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleBuild(scope.row)"
            v-hasPermi="['base:paper:edit']"
          >生成试卷
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleRace(scope.row)"
            v-hasPermi="['base:paper:edit']"
          >开始比赛
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改试卷对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学科" prop="subject">
              <el-select
                v-model="form.subject"
                placeholder="学科"
                clearable
                style="width: 210px;"
              >
                <el-option
                  v-for="dict in dict.type.subject"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="年级" prop="grade">
              <el-select
                v-model="form.grade"
                placeholder="年级"
                clearable
                multiple
                style="width: 600px;"
              >
                <el-option
                  v-for="dict in dict.type.grade"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="left">必答题--各题数量应是参赛人员的整数倍</el-divider>
        <el-row>
          <el-col :span="8">
            <el-form-item label="填空题数量" prop="mustFillNum">
              <el-input type="number" v-model="form.mustFillNum" placeholder="请输入必答填空题数量"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="填空题分值" prop="mustFillScore">
              <el-input type="number" v-model="form.mustFillScore" placeholder="请输入必答填空题分值"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="填空题时限" prop="mustFillTime">
              <el-input type="number" v-model="form.mustFillTime" placeholder="请输入必答填空题时限">
                <el-button slot="append">秒</el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="选择题数量" prop="mustChoiceNum">
              <el-input type="number" v-model="form.mustChoiceNum" placeholder="请输入必答选择题数量"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="选择题分值" prop="mustChoiceScore">
              <el-input type="number" v-model="form.mustChoiceScore" placeholder="请输入必答选择题分值"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="填空题时限" prop="mustChoiceTime">
              <el-input type="number" v-model="form.mustChoiceTime" placeholder="请输入必答选择题时限">
                <el-button slot="append">秒</el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="背诵题数量" prop="mustSayNum">
              <el-input type="number" v-model="form.mustSayNum" placeholder="请输入必答背诵题数量"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="背诵题分值" prop="mustSayScore">
              <el-input type="number" v-model="form.mustSayScore" placeholder="请输入必答背诵题分值"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="填空题时限" prop="mustSayTime">
              <el-input type="number" v-model="form.mustSayTime" placeholder="请输入必答背诵题时限">
                <el-button slot="append">秒</el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="赏析题数量" prop="mustExplainNum">
              <el-input type="number" v-model="form.mustExplainNum" placeholder="请输入必答赏析题数量"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="赏析题分值" prop="mustExplainScore">
              <el-input type="number" v-model="form.mustExplainScore" placeholder="请输入必答赏析题分值"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="填空题时限" prop="mustExplainTime">
              <el-input type="number" v-model="form.mustExplainTime" placeholder="请输入必答赏析题时限">
                <el-button slot="append">秒</el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="left">抢答题</el-divider>
        <el-row>
          <el-col :span="8">
            <el-form-item label="填空题数量" prop="lootFillNum">
              <el-input type="number" v-model="form.lootFillNum" placeholder="请输入抢答填空题数量"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="填空题分值" prop="lootFillScore">
              <el-input type="number" v-model="form.lootFillScore" placeholder="请输入抢答填空题分值"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="填空题时限" prop="lootFillTime">
              <el-input type="number" v-model="form.lootFillTime" placeholder="请输入抢答填空题时限">
                <el-button slot="append">秒</el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="选择题数量" prop="lootChoiceNum">
              <el-input type="number" v-model="form.lootChoiceNum" placeholder="请输入抢答选择题数量"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="选择题分值" prop="lootChoiceScore">
              <el-input type="number" v-model="form.lootChoiceScore" placeholder="请输入抢答选择题分值"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="填空题时限" prop="lootChoiceTime">
              <el-input type="number" v-model="form.lootChoiceTime" placeholder="请输入抢答选择题时限">
                <el-button slot="append">秒</el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="背诵题数量" prop="lootSayNum">
              <el-input type="number" v-model="form.lootSayNum" placeholder="请输入抢答背诵题数量"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="背诵题分值" prop="lootSayScore">
              <el-input type="number" v-model="form.lootSayScore" placeholder="请输入抢答背诵题分值"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="填空题时限" prop="lootSayTime">
              <el-input type="number" v-model="form.lootSayTime" placeholder="请输入抢答背诵题时限">
                <el-button slot="append">秒</el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="赏析题数量" prop="lootExplainNum">
              <el-input type="number" v-model="form.lootExplainNum" placeholder="请输入抢答赏析题数量"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="赏析题分值" prop="lootExplainScore">
              <el-input type="number" v-model="form.lootExplainScore" placeholder="请输入抢答赏析题分值"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="填空题时限" prop="lootExplainTime">
              <el-input type="number" v-model="form.lootExplainTime" placeholder="请输入抢答赏析题时限">
                <el-button slot="append">秒</el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listPaper, getPaper, delPaper, addPaper, updatePaper, buildPaper} from "@/api/base/paper";

export default {
  name: "Paper",
  dicts: ['grade', 'subject'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 试卷表格数据
      paperList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: "",
        subject: "",
        grade: ""
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询试卷列表 */
    getList() {
      this.loading = true;
      listPaper(this.queryParams).then(response => {
        this.paperList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        paperId: null,
        name: "",
        subject: "1",
        grade: ["2"],
        mustFillNum: 1,
        mustFillScore: 1,
        mustChoiceNum: 1,
        mustChoiceScore: 1,
        mustSayNum: 1,
        mustSayScore: 1,
        mustExplainNum: 1,
        mustExplainScore: 1,
        lootFillNum: 1,
        lootFillScore: 1,
        lootChoiceNum: 1,
        lootChoiceScore: 1,
        lootSayNum: 1,
        lootSayScore: 1,
        lootExplainNum: 1,
        lootExplainScore: 1,
        mustFillTime: 30,
        mustChoiceTime: 30,
        mustSayTime: 30,
        mustExplainTime: 30,
        lootFillTime: 30,
        lootChoiceTime: 30,
        lootSayTime: 30,
        lootExplainTime: 30
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.paperId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加试卷";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const paperId = row.paperId || this.ids
      getPaper(paperId).then(response => {
        this.form = response.data;
        this.form.grade = this.form.grade.split(",");
        this.open = true;
        this.title = "修改试卷";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.grade = this.form.grade.join(",");
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.paperId != null) {
            updatePaper(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPaper(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const paperIds = row.paperId || this.ids;
      this.$modal.confirm('是否确认删除试卷编号为"' + paperIds + '"的数据项？').then(function () {
        return delPaper(paperIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('base/paper/export', {
        ...this.queryParams
      }, `paper_${new Date().getTime()}.xlsx`)
    },
    handleBuild(row) {
      const paperIds = row.paperId || this.ids;
      this.$modal.confirm('是否确认生成试卷？').then(function () {
        return buildPaper(paperIds);
      }).then(() => {
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {
      });
    },
    handleRace(row) {
      window.open("/race/" + row.paperId);
    }
  }
};
</script>
